using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Easycmd
{
    public partial class SearchFile : Form
    {
        public SearchFile()
        {
            InitializeComponent();
            CancelButton = btnCancel;
            FileViewUtils.InitListView2(this.lvFileList);
        }
        public static string get(string dir)
        {
            SearchFile ib = new SearchFile();
            ib.tbFileName.Text = "*.*";
            ib.tbDir.Text = dir;
            ib.Height = 155;
            ib.tbFileName.SelectAll();
            ib.tbFileName.Select();
            if (ib.ShowDialog() == DialogResult.OK)
            {
                if (ib.lvFileList.SelectedIndices.Count > 0)
                {
                    return FileUtils.CheckSlash(
                        ib.lvFileList.SelectedItems[0].Text )+
                        ib.lvFileList.SelectedItems[0].SubItems[1].Text ;
                }
                return "";
            }
            else
            {
                return "";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            lvFileList.Items.Clear();
            string filename = tbFileName.Text ;
            string dir = tbDir.Text;
            object[] files = FileUtils.SearchFile(filename, dir);            
            foreach (FileInfo file in files)
            {
                FileViewUtils.FillFile2(this.lvFileList, file.FullName);
            }
            Height = 300;
            lvFileList.Focus();
            if (lvFileList.Items.Count > 0)
            {
                lvFileList.SelectedIndices.Add(0);
            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            
        }

        private void lvFileList_KeyUp(object sender, KeyEventArgs e)
        {
            
        }

        private void lvFileList_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
                this.DialogResult = DialogResult.OK;
        }

        private void SearchFile_Load(object sender, EventArgs e)
        {
  
            
        }
    }
}